import MapFeature from "./MapFeature";
import * as events from '../event';
import * as mapUtil from '../util'; 
import * as ftypes from '../featureTypes'
import {mapManager} from '../MapManager';
import { message } from "ant-design-vue";
import * as api from '../../../util/api';
import Feature from 'ol/Feature';
import {Point} from 'ol/geom'
import Base from './base'
import FacilityStyle from "../style/facilityStyle";


/**
 *  设施信息
 */
class Facilities extends MapFeature{ 
 
    static ROOM_INDEX = 0;
    

    constructor(f,valueObject){
        super(f);
        if (valueObject) {
            this.setValueOjbect(valueObject);
        } 
        this.setType(ftypes.FACILITIES);
    } 

    /**
     * 开始标记地块
     */
    static beginDraw(){ 
        MapFeature.innerDraw({
            ftype:ftypes.FACILITIES,
            type:'Point',
            style:FacilityStyle.drawfacility()
        }
        ,(evt)=>{
           
            var base = mapUtil.tunnelInfoInPolygon(evt.feature,mapManager.getLayers().baseSource);
            if(base == null){
                message.error("设施需要规划在基地中!");
                evt.feature.set("deleted",true); //标记已经删除，后面添加到source后再删除
                mapManager.clearDrawSource();
                return ;
            }
            var baseWrapper = new Base(base);
            var baseInfo = baseWrapper.getValueObject();
            var room = new Facilities(evt.feature,{
                id:null,
                baseInfo:baseInfo
            });
            Facilities.ROOM__INDEX++;
            events.fire(events.types.onAddFacilities,{
                type:'create',
                data:room
            });   
            
        }); 
    } 
    
    /**
     * 开始编辑
     */
    doEdit(){ 
        //通知事件
        events.fire(events.types.onAddFacilities,{type:'update',data:this});
        var _this = this;
        MapFeature.editFeature(this.feature,(evt)=>{
            var feature_temp = evt.features.getArray()[0];
            var base = mapUtil.tunnelInfoInPolygon(feature_temp,mapManager.getLayers().baseSource);
            if(base == null){ 
                message.error("设施需要规划在基地中!");
                return false;
            } 
            
            //通知更新 
            events.fire(events.types.onAddFacilities,{
                type:'update',
                data:_this
            });
        });
     }

    

    /**
     * 初始化数据
     */
    static initData(){
        
        
        api.get(api.ASSET_FACILITIES_LIST,(resp)=>{
            var items = resp.data;
             //加载数据
             for(var i = 0,j = items.length ; i < j ; i++ ){
                var item = items[i];
                if(typeof(item.coordinateGroup) == "undefined" ||  item.coordinateGroup == "" 
                    ||  item.coordinateGroup == null){
                    continue;
                }
                var coordinateGroup = null;
                try{
                    coordinateGroup = JSON.parse(item.coordinateGroup); 
                }catch(e){coordinateGroup = null;}
                
                

                if(coordinateGroup == null) continue; 

                var polygon = new Feature({
                        ftype : ftypes.FACILITIES,
                        geometry: new Point(coordinateGroup.coordinates),
                        name: item.name
                });

                var facilityWrapper = new Facilities(polygon,item);
                facilityWrapper.setId("tunnel_info_" + i)
                mapManager.getLayers().facilitiesSource.addFeature(polygon); 
                facilityWrapper.setStyle(FacilityStyle.getStyle(item));                      
            }
 
            Facilities.ROOM_INDEX = items.length; 
        });
    }

   
    /**
     *  ajax 保存数据
     */
    save(data){       
        this.innerSave(api.ASSET_FACILITIES_SAVE,data,(resp)=>{
            this.setName(data.name);                 
            this.setType(ftypes.FACILITIES) 
            this.feature.setStyle(FacilityStyle.getStyle(data));
            mapManager.getLayers().facilitiesSource.addFeature(this.feature); 
            this.feature.changed();    
        }); 
    } 
}

export default Facilities;